package top.cardone.api.v1.usercenter.department

import org.apache.commons.lang3.StringUtils
import top.cardone.context.ApplicationContextHolder
import top.cardone.core.CodeException
import top.cardone.usercenter.service.DepartmentService

class c0001 implements java.io.Serializable {
    def input(input) {
        def newInput = [:]

        newInput['parentCode'] = input['parentCode']
        newInput['departmentCode'] = input['departmentCode']
        newInput['name'] = input['name']
        newInput['flagCode'] = StringUtils.defaultIfBlank(input['flagCode'], "input")

        newInput
    }

    def validation(input) {
//      if (StringUtils.isBlank(input?.departmentCode)) {
//          throw new CodeException("departmentCode required", "部门编号必填")
//      }
//
//      if (StringUtils.length(input?.departmentCode) < 4) {
//          throw new CodeException("departmentCode minlength", "部门编号需{0}个字符以上", 4)
//      }
//
//      if (StringUtils.length(input?.departmentCode) > 255) {
//          throw new CodeException("departmentCode maxlength", "部门编号需小于{0}个字符", 255)
//      }
//
//      if (StringUtils.isBlank(input?.name)) {
//          throw new CodeException("name required", "部门名称必填")
//      }
//
//      if (StringUtils.length(input?.name) < 4) {
//          throw new CodeException("name minlength", "部门名称需{0}个字符以上", 4)
//      }
//
//      if (StringUtils.length(input?.name) > 255) {
//          throw new CodeException("name maxlength", "部门名称需小于{0}个字符", 255)
//      }

        def readOne = ['departmentCode': input.departmentCode]

        def count = ApplicationContextHolder.getBean(DepartmentService.class).readOne(Integer.class, readOne)

        if (count > 0) {
            throw new CodeException("departmentCode already exists", "部门编号已经存在")
        }
    }

    def func(input) {
        ['insertCounts': ApplicationContextHolder.getBean(DepartmentService.class).insertByNotExistsCache(input)]
    }

    def output(output) {
        output
    }
}